package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.base.pojo.page.LayuiPageFactory;
import com.hippy.erya.base.pojo.page.LayuiPageInfo;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.impl.RawMaterialDictionaryServiceImpl;
import com.hippy.erya.query.BaseQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/erya/rawmaterialdictionary")
public class RawMaterialDictionaryController {

    @Autowired
    private RawMaterialDictionaryServiceImpl rawMaterialDictionaryService;

    @RequestMapping(value = "/rawmaterialdictionary")
    public String rawmaterialdictionary() {
        return "/erya/rawmaterialdictionary/rawmaterialdictionary_setting.html";
    }

    @RequestMapping(value = "/rawmaterialdictionaryAdd")
    public String rawmaterialdictionaryAdd() {
        return "/erya/rawmaterialdictionary/rawmaterialdictionary_add.html";
    }

    @RequestMapping(value = "/rawmaterialdictionaryEdit")
    public String rawmaterialdictionaryEdit() {
        return "/erya/rawmaterialdictionary/rawmaterialdictionary_edit.html";
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public LayuiPageInfo queryByPage(@PathParam("page") Long page, @PathParam("limit") int limit,
                                     @PathParam("name") String name) {
        return LayuiPageFactory.createPageInfo(rawMaterialDictionaryService.queryByPage(page, limit, name));
    }

    @RequestMapping("/deleteData/{id}")
    @ResponseBody
    public ResponseData deleteData(@PathVariable("id") int id) {
        RawMaterialDictionaryDO rawMaterialDictionaryDO = rawMaterialDictionaryService.getById(id);
        rawMaterialDictionaryDO.deleteData(LoginContextHolder.getContext().getUserId().toString());
        rawMaterialDictionaryService.updateById(rawMaterialDictionaryDO);
        return ResponseData.success();
    }

    @RequestMapping("/addData")
    @ResponseBody
    public ResponseData addData(RawMaterialDictionaryDO rawMaterialDictionaryDO) {
        if (!checkRawMaterialDictionaryDO(rawMaterialDictionaryDO)) {
            return ResponseData.error("该食材已存在！");
        }
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialDictionaryDO.setUserCode(user);
        rawMaterialDictionaryDO.setCreatedBy(user);
        rawMaterialDictionaryDO.setUpdatedBy(user);
        rawMaterialDictionaryService.save(rawMaterialDictionaryDO);
        return ResponseData.success();
    }


    private boolean checkRawMaterialDictionaryDO(RawMaterialDictionaryDO rawMaterialDictionaryDO) {
        List<RawMaterialDictionaryDO> entity = rawMaterialDictionaryService.queryByCode(rawMaterialDictionaryDO.getCode());
        if (!CollectionUtils.isEmpty(entity)) {
            return false;
        }
        entity = rawMaterialDictionaryService.queryByName(rawMaterialDictionaryDO.getName());
        if (!CollectionUtils.isEmpty(entity)) {
            return false;
        }
        return true;
    }

    @RequestMapping("/editData")
    @ResponseBody
    public ResponseData editData(RawMaterialDictionaryDO rawMaterialDictionaryDO) {
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialDictionaryDO.setUpdatedBy(user);
        rawMaterialDictionaryService.updateById(rawMaterialDictionaryDO);
        return ResponseData.success();
    }


    @RequestMapping("/rawmaterialdictionaryDetail/{id}")
    @ResponseBody
    public ResponseData editData(@PathVariable("id") int id) {
        return ResponseData.success(rawMaterialDictionaryService.getById(id));
    }

    @RequestMapping("/listAdd")
    @ResponseBody
    public ResponseData listAdd() {
        QueryWrapper<RawMaterialDictionaryDO> wrapper = BaseQueryWrapper.buildQuery();
        return ResponseData.success(rawMaterialDictionaryService.list(wrapper));
    }


}
